package com.tsinkai.ettp.controller;


import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Enumeration;
import java.util.Map;

import javax.imageio.ImageIO;
import javax.print.attribute.standard.Media;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.tsinkai.ettp.common.ResourceCustom;
import com.tsinkai.ettp.common.ResultModel;

@RestController
public class HelloController {
	
	@Autowired
	ResourceCustom resource;
	
	@RequestMapping(value="wly120",produces = MediaType.IMAGE_PNG_VALUE)
	public byte[] helloTest() throws IOException {
		InputStream is = this.getClass().getResourceAsStream("/static/img/001.png");
		byte[] bytes = null;
		try {
			bytes = new byte[is.available()];
			is.read(bytes,0,is.available());
		} catch (IOException e) {
			e.printStackTrace();
		}
		BufferedImage bi = null;
			bi = ImageIO.read(is);
		if(bi==null) {
			System.out.println("null");
		}
		return bytes;
	}
	
	@RequestMapping("hello")
	public String helloTest(HttpServletRequest req,HttpServletResponse resp) {
		System.out.println(req.getLocalName());
		System.out.println(req.getLocalAddr());
		System.out.println(req.getServerPort());
		
		System.out.println(req.getServerName());
		System.out.println(req.getServerPort());
		
		
		return "ngnix test localIp:"+req.getLocalAddr()+"serverName:"+req.getServerName();
	}
	
	@RequestMapping("/static/index.htm")
	public String hello() {
		
		
		return "hello,springboot";
	}
	

	
	@RequestMapping("/testResource")
	public ResultModel testResource() {
		System.out.println(resource.getName());
		ResourceCustom bean = new ResourceCustom();
		BeanUtils.copyProperties(resource, bean);
		
		return ResultModel.ok(bean);
	}
	
	@RequestMapping("/testp")
	public ResultModel testo( String loginName) {
		//打印所有key-value
//				for(Map.Entry<String, Object> entry : map.entrySet()) {
//					System.out.println("key:"+entry.getKey()+",value:"+entry.getValue());
//				}
//				String login = (String) map.get("loginName");
				System.out.println("*****:"+loginName);
		return ResultModel.ok(loginName);
		
	}
	
//	@RequestMapping("testp")
	public ResultModel testp( HttpServletRequest request) throws IOException {
		 String body = null;  
		    StringBuilder stringBuilder = new StringBuilder();  
		    BufferedReader bufferedReader = null;  
		    try {  
		        InputStream inputStream = request.getInputStream();  
		        if (inputStream != null) {  
		            bufferedReader = new BufferedReader(new InputStreamReader(inputStream));  
		            char[] charBuffer = new char[128];  
		            int bytesRead = -1;  
		            while ((bytesRead = bufferedReader.read(charBuffer)) > 0) {  
		                stringBuilder.append(charBuffer, 0, bytesRead);  
		            }  
		        } else {  
		            stringBuilder.append("");  
		        }  
		    } catch (IOException ex) {  
		        throw ex;  
		    } finally {  
		        if (bufferedReader != null) {  
		            try {  
		                bufferedReader.close();  
		            } catch (IOException ex) {  
		                throw ex;  
		            }  
		        }  
		    }  
		  
		    body = stringBuilder.toString();  	
		    System.out.println("&&&&"+body);
		
		Map<String, String[]> map = request.getParameterMap();
		//打印所有key-value
		for(Map.Entry<String, String[]> entry : map.entrySet()) {
			System.out.println("key:"+entry.getKey()+",value"+entry.getValue()[0]);
		}
		//打印key
		Enumeration<String> enums = request.getParameterNames();
		while(enums.hasMoreElements()){
			String name = enums.nextElement();
			System.out.println("key:"+name);
		}
		
		String loginName = request.getParameter("loginName");
		System.out.println("*******"+loginName);
		return ResultModel.ok(map);
	}
}
